<?php

namespace Api\Sms\Models;

use Api\ModelExtTrait;
use Illuminate\Database\Eloquent\Model;

/**
 * 模型
 */
class SmsVerificationCode extends Model {

    use ModelExtTrait;

    /**
     * 当前表名
     */
    protected $table = 'member__verification_codes';

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['confine_time'];

    /**
     * 数据校验规则
     * @var array
     */
    public $rules = [
        'mobile' => [
            'methods' => 'regexMobile',
        ],
    ];

    protected $visible = [
        'country_code',
        'mobile',
        'type',
        'code',
        'hash',
        'hash_fail',
        'created_at'
    ];


/*
|--------------------------------------------------------------------------
| 拓展校验方法
|--------------------------------------------------------------------------
*/
    public function regexMobile($value)
    {
        $config = config('Sms\ZhuTong');
        $config = $config['mobile'];

        if (! $value) {
            return $config['required'];
        }
        
        if (preg_match($config['pattern'], $value) === 0) {
            return $config['regex'];
        }
    }
}
